package m.meiweitech.com.rocket.ext

import android.app.Activity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus
import permissions.dispatcher.ktx.PermissionsRequester;


/**
 * @author WEN
 * @Description:
 * @date 2020/12/21 14:28
 */

fun <T:Any> Any.postObject(key: String, entity: T = Object() as T) {
    LiveEventBus
        .get(key)
        .post(entity)
}

fun <T> Any.postDelayObject(key: String, entity: T = Object() as T, delay: Long) {
    LiveEventBus
        .get(key)
        .postDelay(entity, delay)
}


fun <T> Any.observeObject(key: String, owner: LifecycleOwner, clazz: Class<T>, accept: T.() -> Unit) {
    LiveEventBus
        .get(key, clazz)
        .observe(owner, Observer<T> {
            it.accept()
        })
}

fun <T> Any.observeObject1(key: String, owner: LifecycleOwner, clazz: Class<List<T>>, accept: List<T>.() -> Unit) {
    LiveEventBus
        .get(key, clazz)
        .observe(owner, Observer<List<T>> {
            it.accept()
        })
}

fun  <T> Any.observeObjectSticky(key: String, owner: LifecycleOwner, clazz: Class<T>,  isRemove:Boolean = false , accept: T.() -> Unit) {
    val observer = Observer<T> {
        accept(it)
    }
    LiveEventBus
        .get(key, clazz)
        .observeSticky(owner, observer)
    if(isRemove){
        LiveEventBus
            .get(key, clazz).removeObserver(observer)
    }
}

fun <T> LifecycleOwner.observeObject(key: String, clazz: Class<T>, accept: T.() -> Unit) {
    LiveEventBus
        .get(key, clazz)
        .observe(this, {
            it.accept()
        })
}
